qui(function(require, exports, module) {
    Server.Edit = function(options) {
        this.dao = Server.Dao;
        this.options = $.extend(true, {}, options);

        this.util = Server.Util || {};
        this.config = Server.config || {};
    };

    Server.Edit.prototype = {
        oldIdc:'',
        initConfig: function() {
			return true;
		},
        init: function() {
        },

        run: function() {
            var self = this;
            var $container = self.options.$container;
            self.init();
            self.initConfig();

			self.bindEvent();
			self.perpareData();
            self.fillForm();
        },

        perpareData: function() {
            var self = this;
            var username = $("div.user a.name").html();
            Server.Util.setProvince();
        },

        fillForm: function () {
            var id = Server.Util.getUrlId("id");
            var self = this;
            if (id != null) {
                $("input[name=sn]").attr("disabled","disabled");
                self.dao.getList({id: id}, function (data) {
                    var retValue = null;
                    if (data.code === "A00000" && data.data.result != null) {
                        retValue = data.data.result[0];
                        console.log(retValue)
                        self.fillData(retValue);
                        self.oldIdc = retValue['idc'];
                    } else {
                        self.fillNoData();
                    }
                });
            } else {
                self.fillNoData();
            }
        },

        fillData: function (element) {
            var self = this;
            for (var key in element) {
                if (key == "status" || key == "serverType" || key =="city" || key == "idc") {
                    self.options.$container.find("select[name=" + key + "]").val(element[key]);
                    if(key=='serverType'&&element[key]==Server.Config.INCLUDE_TYPE){
                        self.options.$container.find('#include').css('display','block');
                    }

                } else if (key == "province"){
                    self.options.$container.find("select[name=" + key + "]").val(element[key]);
                    self.options.$container.find("select[name=" + key + "]").trigger("change");
                    self.options.$container.find("select[name=city]").val(element["city"]);
                } else {
                    self.options.$container.find("input[name=" + key + "]").val(element[key]);
                }
            }
        },
        fillNoData: function () {
            var self = this;
        },

        bindEvent: function() {
            var self = this;
            var validator = self.getValidator();
            var $container = self.options.$container;
            var $submit = $container.find('#Submit');
            var $type = $container.find('select[name="serverType"]');
            var $include = $container.find('#include');

            $submit.on('click', function() {
                $submit.html('提交中..');
                $submit.attr('disabled','disabled');
                var _save = function(data) {
                    $submit.val('提交中..');
                    if ($('#id').val() != "") {
                        self.edit(data);
                    } else {
                        self.add(data);
                    }
                };
				var success = validator.form();
                var data = self.getParam();

				if (success ){
					_save(data);
				}else{
                    $submit.attr('disabled',false);
                    $submit.html('提交');
                }

            });

            $type.change(function(){
                //新收录
                if($(this).val() == 12){
                    $include.css('display','block');
                }else{
                    $include.css('display','none');
                };
            });


        },

        getValidator: function() {
            var $container = this.options.$container;
            var $include = $container.find("#include");
            var $form = $container;
            if($include.css('display')=='none'){
                $include.find('input').val(0);
            }

            var validator = Validator.getValidator($form, Server.Config.validator);

            return validator;
        },

        getParam: function() {
            var $container = this.options.$container;
            var self = this;
			var checkboxObj = null;
            var $elements = $.makeArray($container.prop('elements')).filter(function(item) {

				return true;
            });

			var checkboxVal = [];
            var name;
            var key;
			var params = {};
            var flag = 0;

            $.each($elements, function() {
                name = this.name;
                if (name) {
                    if(name=='id' && $(this).val() == "") {

                    } else if(name =='idc'){
                        if(self.oldIdc!='' && self.oldIdc!= $(this).val()){
                            params['cluster'] = -1; //前后集群不一
                        }
                        params[name] = $(this).val();
                    }else {
                        params[name] = $(this).val().trim();
                    }
                }
            });


            return {entity : JSON.stringify(params) };
        },
        add: function(data) {
            this.dao.add(data, this.onSave);
        },
        edit: function(data) {
            this.dao.edit(data, this.onSave);
        },

        onSave: function(json) {
            var $submit = $('#Submit');
            setTimeout(function() {
                $submit.html('提交');
                $submit.attr('disabled',false);
            }, 750);
            if (json.code == 'A00000') {
                var html = '操作成功';
                $(document).tips({
                    content: html,
                    type: 'success',
                });
            } else {
                $(document).tips({
                    content: json.msg,
                    type: "error"
                });
            }

        },

    };

});
